package com.carkeeper.shop.check.service.impl;
import com.carkeeper.entity.Result;
import com.carkeeper.entity.StatusCode;
import com.carkeeper.shop.check.service.CheckStoreService;
import com.carkeeper.shop.check.dao.StoreDetailMapper;
import com.carkeeper.shop.check.dao.StoreMapper;
import com.carkeeper.shop.pojo.Store;
import com.carkeeper.shop.pojo.StoreDetail;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;
import java.util.Collections;
import java.util.List;
import java.util.Map;
@Service
public class CheckStoreServiceImpl implements CheckStoreService {
    @Autowired
    private StoreMapper storeMapper;
    @Autowired
    private StoreDetailMapper storeDetailMapper;

    /**
     * 分页展示
     *
     * @param searchMap
     * @param page
     * @param size
     * @return
     */
    @Override
    public Page<Store> findPage(Map searchMap, String page, String size) {
        int pageNum;
        int pageSize;
        if (page == null) {
            pageNum = 1;
        }
        pageNum = Integer.parseInt(page);
        if (size == null) {
            pageSize = 15;
        }
        pageSize = Integer.parseInt(size);
        PageHelper.startPage(pageNum, pageSize);
        Example example = new Example(Store.class);
        Example.Criteria criteria = example.createCriteria();
        //主体名称（门店简称）
        if (searchMap.get("simpleName") != null && !"".equals(searchMap.get("simpleName"))) {
            criteria.andLike("simpleName", "%" + (String) searchMap.get("simpleName") + "%");
        }
        //门店编码
        if (searchMap.get("code") != null && !"".equals(searchMap.get("code"))) {
            criteria.andEqualTo("code", searchMap.get("code"));
        }
        //类别
        if (searchMap.get("type") != null && !"".equals(searchMap.get("type"))) {
            criteria.andEqualTo("type", searchMap.get("type"));
        }
        //是否连锁
        if (searchMap.get("isChain") != null && !"".equals(searchMap.get("isChain"))) {
            criteria.andEqualTo("isChain", searchMap.get("isChain"));
        }
        //省市区
        if (searchMap.get("province") != null && !"".equals(searchMap.get("province"))) {
            criteria.andEqualTo("province", searchMap.get("province"));
        }
        if (searchMap.get("city") != null && !"".equals(searchMap.get("city"))) {
            criteria.andEqualTo("city", searchMap.get("city"));
        }
        if (searchMap.get("county") != null && !"".equals(searchMap.get("county"))) {
            criteria.andEqualTo("county", searchMap.get("county"));
        }
        //商家为审核状态时才展示
        criteria.andEqualTo("checkd", "0");
        List<Store> stores = storeMapper.selectByExample(example);
        //倒排
        Collections.reverse(stores);
        return (Page<Store>) stores;
    }

    /**
     * 展示待审核服务商信息
     *
     * @param id
     * @return
     */
    @Override
    public Store show(String id) {
        if (id == null || "".equals(id)) {
            return null;
        }
        //查询数据库，获取当前的商家信息
        Store store = storeMapper.selectByPrimaryKey(id);
        //根据商家的id获取商家商家详情的图片
        StoreDetail storeDetail = storeDetailMapper.selectByPrimaryKey(store.getId());
        if (storeDetail != null) {
            store.setStoreDetail(storeDetail);
        }
        return store;
    }

    /**
     * 已拒绝服务商主体
     *
     * @param searchMap
     * @param page
     * @param size
     * @return
     */
    @Override
    public Page<Store> turn(Map searchMap, String page, String size) {
        int pageNum;
        int pageSize;
        if (page == null) {
            pageNum = 1;
        }
        pageNum = Integer.parseInt(page);
        if (size == null) {
            pageSize = 15;
        }
        pageSize = Integer.parseInt(size);
        PageHelper.startPage(pageNum, pageSize);
        Example example = new Example(Store.class);
        Example.Criteria criteria = example.createCriteria();
        //主体名称（门店简称）
        if (searchMap.get("simpleName") != null && !"".equals(searchMap.get("simpleName"))) {
            criteria.andLike("simpleName", "%" + (String) searchMap.get("simpleName") + "%");
        }
        //门店编码
        if (searchMap.get("code") != null && !"".equals(searchMap.get("code"))) {
            criteria.andEqualTo("code", searchMap.get("code"));
        }
        //类别
        if (searchMap.get("type") != null && !"".equals(searchMap.get("type"))) {
            criteria.andEqualTo("type", searchMap.get("type"));
        }
        //是否连锁
        if (searchMap.get("isChain") != null && !"".equals(searchMap.get("isChain"))) {
            criteria.andEqualTo("isChain", searchMap.get("isChain"));
        }
        //省市区
        if (searchMap.get("province") != null && !"".equals(searchMap.get("province"))) {
            criteria.andEqualTo("province", searchMap.get("province"));
        }
        if (searchMap.get("city") != null && !"".equals(searchMap.get("city"))) {
            criteria.andEqualTo("city", searchMap.get("city"));
        }
        if (searchMap.get("county") != null && !"".equals(searchMap.get("county"))) {
            criteria.andEqualTo("county", searchMap.get("county"));
        }
        //商家为审核状态时才展示
        criteria.andEqualTo("checkd", "2");
        List<Store> stores = storeMapper.selectByExample(example);

        return (Page<Store>) stores;
    }

    /**
     * 服务商审核
     *
     * @param id
     * @param status
     */
    @Override
    public Result check(String id, String status) {
        if (id == null || "".equals(id)) {
            throw new RuntimeException("服务商id不能为null");
        }
        //1.先查询当前服务商是否存在
        Store store = storeMapper.selectByPrimaryKey(id);
        if (store == null || "".equals(status)) {
            throw new RuntimeException("服务商信息不存在");
        }
        //2.判断当前服务商的状态是否是待审核的状态
        if (!"0".equals(store.getCheck())) {
            throw new RuntimeException("当前服务商已进行审核");
        }
        store.setCheck(status);
        storeMapper.updateByPrimaryKeySelective(store);
        //3.对服务商进行审核，返回结果
        return new Result(true, StatusCode.OK, "商家已审核");
    }
}
